package com.cyandpos.pad.attachselect.model;

import android.text.TextUtils;


import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.GetValueCardResponse;
import com.cyandpos.pad.posdata.GetVipCouponResponse;
import com.cyandpos.pad.posdata.QueryMemberInfoResponse;
import com.cyandpos.pad.posdata.QueryMemberRequest;
import com.google.gson.JsonObject;


public class MemberModelImpl implements IMemberModel {

    @Override
    public void queryMemberDetail(QueryMemberRequest requestParam, final onQueryMemberDetailListener listener) {

        PosHttpManager.QueryMemberInfoExecute(requestParam, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<QueryMemberInfoResponse> baseResponse = BaseResponse.fromJson(response.toString(),QueryMemberInfoResponse.class);
                if (baseResponse.getCode().equals("0")) {
                    try {
                        listener.onQueryMemberDetailSuccess(baseResponse.getData());
                    }catch (Exception e){
                        listener.onQueryMemberDetailFail(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }
                }else{
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onQueryMemberDetailFail(baseResponse.getMsg());
                    }else {
                        listener.onQueryMemberDetailFail(baseResponse.getTips());
                    }
                }
            }
            @Override
            public void onFailure(String msg) {
                listener.onQueryMemberDetailFail(msg);
            }
        });
    }

    @Override
    public void queryMemberCoupon(QueryMemberRequest requestParam, final onQueryMemberCouponListener listener) {
        PosHttpManager.QueryMemberCouponExcute(requestParam, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<GetVipCouponResponse> baseResponse = BaseResponse.fromJson(response.toString(),GetVipCouponResponse.class);
                if (baseResponse.getCode().equals("0")) {
                    try {
                        listener.onQueryMemberCouponSuccess(baseResponse.getData());
                    }catch (Exception e){
                        listener.onQueryMemberCouponFail(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }

                } else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onQueryMemberCouponFail(baseResponse.getMsg());
                    }else {
                        listener.onQueryMemberCouponFail(baseResponse.getTips());
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onQueryMemberCouponFail(msg);
            }
        });
    }


    @Override
    public void queryValueCard(QueryMemberRequest requestParam, final onQueryValueCardListener listener) {
        PosHttpManager.QueryCashCard(requestParam, new OkHttpUtils.ResultCallback() {
            @Override
            public void onSuccess(JsonObject response) {
                BaseResponse<GetValueCardResponse> baseResponse = BaseResponse.fromJson(response.toString(),GetValueCardResponse.class);
                if (baseResponse.getCode().equals("0")) {
                    try {
                        listener.onQueryValueCardSuccess(baseResponse.getData());
                    }catch (Exception e){
                        listener.onQueryValueCardFail(baseResponse.getCode() + baseResponse.getMsg() + e.getMessage().toString());
                    }

                } else {
                    if (TextUtils.isEmpty(baseResponse.getTips())){
                        listener.onQueryValueCardFail(baseResponse.getMsg());
                    }else {
                        listener.onQueryValueCardFail(baseResponse.getTips());
                    }
                }
            }

            @Override
            public void onFailure(String msg) {
                listener.onQueryValueCardFail(msg);
            }
        });
    }


}
